package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import model.TBackLog;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import persistent.factory.DAOFactory;
import util.toolkit.Expression;

/**
 * 后台日志管理处理类
 * @author 徐震霖
 * @since 2019-03-03
 */
@Controller
@RequestMapping(value="/backlogmanagement")
public class SysBackLogManagementController {

	/**
	 * 获取后台操作日志
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 获取后台操作日志
	@RequestMapping(value="/getbackloglist")
	public String getBackLogList(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression exp = new Expression();
		
		int currentPage = 1;
		int pagesize = 10;
		
		try {					
			String page = req.getParameter("page");
			if(page != null && !page.equals("")){
				currentPage = Integer.parseInt(page);
			}
			String limit = req.getParameter("limit");
			if(limit != null && !limit.equals("")){
				pagesize = Integer.parseInt(limit);
			}
			String operationType = req.getParameter("operationtype");
			if(operationType != null && !operationType.equals("")){
				exp.andEqu("operationType", operationType, String.class);
			}
			String realName = req.getParameter("realname");
			if (realName != null && !realName.equals("")) {
				exp.andLike("realName", realName, String.class);
			}
			exp.orderByDesc("operationTime");
			List<TBackLog> backLogsize = DAOFactory.getIBackLogDAO().getBackLogs(exp.toString());
			List<TBackLog> backLogs = DAOFactory.getIBackLogDAO().getBackLogsByPage(exp.toString(), currentPage, pagesize);
			if (backLogs != null && backLogs.size() > 0) {
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", backLogsize.size());
				JSONArray jsonArray = JSONArray.fromObject(backLogs);
				result.element("data", jsonArray);
			}
			else {
				result.put("code", 1);
				result.put("msg", "未获取到日志信息！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "出现异常，日志信息获取失败，请重试！");
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
}
